﻿using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Framework
{

    public abstract class UIFormLogic : MonoBehaviour 
    {
        
        public IUIForm UIForm
        {
            get;
            set;
        }
        public const int DepthFactor = 100;
        private Canvas m_CachedCanvas = null;
        private CanvasGroup m_CanvasGroup = null;
        private List<Canvas> m_CachedCanvasContainer = new List<Canvas>();

        public void Close()
        {
            GameEntry.GetComponent<UIManager>().CloseUIForm(UIForm,null);
        }
        public int OriginalDepth
        {
            get;
            private set;
        }

        public int Depth
        {
            get
            {
                return m_CachedCanvas.sortingOrder;
            }
        }


        protected internal virtual void OnInit(object userData)
        {
            // base.OnInit(userData);

            m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
            m_CachedCanvas.overrideSorting = true;
            OriginalDepth = m_CachedCanvas.sortingOrder;

            m_CanvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();

            RectTransform transform = GetComponent<RectTransform>();
            transform.anchorMin = Vector2.zero;
            transform.anchorMax = Vector2.one;
            transform.anchoredPosition = Vector2.zero;
            transform.sizeDelta = Vector2.zero;

            gameObject.GetOrAddComponent<GraphicRaycaster>();
            SetFont();
            //添加关闭按钮,第一个对象

        }

        public static TMP_FontAsset font;
        void SetFont()
        {
            var allTmpText = transform.GetAllComponent<TMP_Text>();
            var allText = transform.GetAllComponent<Text>();
            foreach (var tmpText in allTmpText)
            {
                tmpText.font = font;
            }

            foreach (var text in allText)
            {
                text.font = font.sourceFontFile;
            }
        }

        public virtual void OnOpen(object userData)
        {
            gameObject.SetActive(true);
        }

        public virtual void OnClose(object userData)
        {
            gameObject.SetActive(false);
        }

        public virtual void OnRecycle(object userData){}
        public virtual void OnUpdate(float elapseSeconds, float realElapseSeconds, object userData){}

        public virtual void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
        {
            int oldDepth = Depth;
            // base.OnDepthChanged(uiGroupDepth, depthInUIGroup);
            int deltaDepth = UIGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth + OriginalDepth;
            GetComponentsInChildren(true, m_CachedCanvasContainer);
            for (int i = 0; i < m_CachedCanvasContainer.Count; i++)
            {
                m_CachedCanvasContainer[i].sortingOrder += deltaDepth;
            }
            m_CachedCanvasContainer.Clear();
        }
    }

}